using System;
using System.Windows.Forms;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;

namespace NDomain.DomainExplorer
{
	/// <summary>The object for implementing an Add-in.</summary>
	/// <seealso class='IDTExtensibility2' />
	public class Connect : IDTExtensibility2, IDTCommandTarget
	{
		/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
		public Connect()
		{
		}

		/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
		/// <param term='application'>Root object of the host application.</param>
		/// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
		/// <param term='addInInst'>Object representing this Add-in.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
		{
			try
    {
        // ctlProgID - the ProgID for your user control.
        // asmPath - the path to your user control DLL.
        // guidStr - a unique GUID for the user control.
        string ctlProgID, asmPath, guidStr;
        // Variables for the new tool window that will hold
        // your user control.
        EnvDTE80.Windows2 toolWins;
        EnvDTE.Window toolWin;
        object control = null;

        _applicationObject = (DTE2)application;
        _addInInstance = (EnvDTE.AddIn)addInInst;
        ctlProgID = typeof(DomainExplorerControl).FullName;
        // Replace the <Path to VS Project> with the path to
        // the folder where you created the WindowsCotrolLibrary.
        // Remove the line returns from the path before 
        // running the add-in.
        asmPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        guidStr = "{E9C60F2B-F01B-4e3e-A551-C09C62E5F584}";

        toolWins = (Windows2)_applicationObject.Windows;
        // Create the new tool window, adding your user control.
        toolWin = toolWins.CreateToolWindow2(_addInInstance, 
          asmPath, ctlProgID, "Domain Explorer", guidStr, 
          ref control);

        ((DomainExplorerControl) control).DTE = _applicationObject;
                
        // The tool window must be visible before you do anything 
        // with it, or you will get an error.
        if (toolWins != null)
        {
            toolWin.Visible = true;
        }

                 
        // Uncomment the code below to set the new tool window's
        // height and width, and to close it.
        // System.Windows.Forms.MessageBox.Show("Setting the height 
        // to 500 and width to 400...");
        // toolWin.Height = 500;
        // toolWin.Width = 400;
        // System.Windows.Forms.MessageBox.Show
        //   ("Closing the tool window...");
        // toolWin.Close(vsSaveChanges.vsSaveChangesNo);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("Exception: " 
          + ex.Message);
    }
		}

	 

	    /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
		/// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
		{
		}

		/// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />		
		public void OnAddInsUpdate(ref Array custom)
		{
		}

		/// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnStartupComplete(ref Array custom)
		{
		}

		/// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnBeginShutdown(ref Array custom)
		{
		}
		
		/// <summary>Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command's availability is updated</summary>
		/// <param term='commandName'>The name of the command to determine state for.</param>
		/// <param term='neededText'>Text that is needed for the command.</param>
		/// <param term='status'>The state of the command in the user interface.</param>
		/// <param term='commandText'>Text requested by the neededText parameter.</param>
		/// <seealso class='Exec' />
		public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
		{
			if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
			{
				if(commandName == "NDomain.DomainExplorer.AddIn.Connect.NDomain.DomainExplorer.AddIn")
				{
					status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
					return;
				}
			}
		}

		/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
		/// <param term='commandName'>The name of the command to execute.</param>
		/// <param term='executeOption'>Describes how the command should be run.</param>
		/// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
		/// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
		/// <param term='handled'>Informs the caller if the command was handled or not.</param>
		/// <seealso class='Exec' />
		public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
		{
			handled = false;
			if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
			{
				if(commandName == "NDomain.DomainExplorer.AddIn.Connect.NDomain.DomainExplorer.AddIn")
				{
					handled = true;
					return;
				}
			}
		}
		private DTE2 _applicationObject;
		private EnvDTE.AddIn _addInInstance;
	}
}